package com.hdy.oa.web.action.department;

import com.hdy.DBUtils.JDBC;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet(value = "/dept/query")
public class DepQueryServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Connection connection=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String contextPath=request.getContextPath();
        String deptno = request.getParameter("deptNo");
        String no=request.getParameter("no");

        out.print("       <!DOCTYPE html>") ;
        out.print("<html>");
        out.print("	<head>");
        out.print("		<meta charset='utf-8'>");
        out.print("		<title>部门修改</title>");
        out.print("	</head>");
        out.print("	<body>");
        out.print("		<h1 align='center'><font size='200pix' color='blue'>部门修改</font></h1>");
        out.print("		<hr>");
        out.print("		<br><br><br>");
        out.print("		<form action='"+contextPath+"/dept/modify'' method='POST'>");
        out.print("			<div align='center' >");



        try {
            connection= JDBC.getConnection();
            String sql="select DEPTNO,DNAME,LOC from dept where DEPTNO=?";
            ps= connection.prepareStatement(sql);
            ps.setString(1,deptno);
            rs = ps.executeQuery();

            if(rs.next()){
                String deptNo=rs.getString("DEPTNO");
                String name=rs.getString("DNAME");
                String loc=rs.getString("LOC");

                out.print("                序号<input type='text'  name='no' value='"+no+"' readonly><br><br>");
                out.print("                部门编号<input type='text' name='deptNo' value='"+deptNo+"'readonly><br><br>");
                out.print("                部门名称<input type='text' name='dName' value='"+name+"'><br><br>");
                out.print("                部门位置<input type='text' name='loc' value='"+loc+"'><br><br>");
                out.print("				<span align='center' ><input type='submit' value='修改'></span>");
                out.print("		<span align='center' >");
                out.print("			<input  type='button' value='返回' onclick='window.history.back()'>");
                out.print("		</span>");

            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBC.close(connection,ps, rs);
        }

        ;out.print("			</div>");
        out.print("		");
        out.print("		</form>");
        out.print("		");
        out.print("	</body>");
        out.print("</html>");
    }
}
